const express = require('express');
const router = express.Router();
const db = require('../../../util/dbConfig/dbConfig')

//页面加载
router.post('/',async (req,res) => {
    //从session中获取用户信息
    let userInfo = req.session.userInfo
    //定义查询结果
    let result
    let countAll
    let {
            sortLowNum,   //第几列的排序数，从左往右，从1开始
            ISAsc,         //增还是减  1为增，0为减
            page,      //页码
        isPage,     //是否分页 1为分页0为不分页
         } = req.body
    if(!page){
        return res.send({
            code: 1,
            msg: '请输入页码'
        })
  }
  console.log(req.body)
    let newPage = (page - 1) * 10 //跳过多页
    //如果用户等级为普通用户
    if(userInfo.admin === 0){
        //获取该用户ID
        let id = userInfo._id
        try{
            let sql = `select
            a.ID, 
            c.NAME,
            a.EWE_ID,
            a.RAM_ID,
            a.BEGIN_DATE,
            a.FINISH_DATE,
            a.PMSG,
            a.PG,
            a.ADOPT_PRECISE,
            a.VITALITY,
            a.DENSITY,
            a.DILUTION_RATIO,
            a.DATE,
            a.HEAT_FIRM1_DATE,
            a.HEAT_FIRM1_NUMBER,
            a.HEAT_FIRM2_DATE,
            a.HEAT_FIRM2_NUMBER,
            a.SPERM_RECORD1,
            a.SPERM_RECORD1_DATE,
            a.SPERM_RECORD2,
            a.SPERM_RECORD2_DATE,
            a.remark
            from hybridization a , sheep b , user c
            where a.EWE_ID = b.ID and c.ID = b.USER_ID and b.USER_ID =? 
            `
            let sqlArr = [id]
            let countSql = `SELECT count(*) FROM  (${sql}) as d`
            //排序查询
            if(sortLowNum !== null && sortLowNum !== '' && ISAsc !== null && ISAsc !== ''){
              if(ISAsc === 1){
                  //升序
                  sql = sql + ' ' + `order by ${sortLowNum} asc`
              }else if(ISAsc === 0){
                  //降序
                  sql = sql + ' ' +`order by ${sortLowNum} desc`
              }
          }
            //分页sql
            if(isPage === 1){
                sql = sql + ' ' +`limit ${newPage},10`
            }

            result = await db.SySqlConnect(sql,sqlArr)
            if(result.length === 0 || !result){
                return res.send({
                    code: 2,
                    msg: '您没有羊'
                })
            }
            countAll = await db.SySqlConnect(countSql,sqlArr)
            countAll = countAll[0]['count(*)']
        }catch (e) {
            console.log(e);
            return res.send({
                code: 4,
                msg: '服务器错误，请稍后再试'
            })
        }
    }else if(userInfo.admin === 1){
        //如果用户权限为管理员，查询结果为空数组
        result = [];
    }

    res.send({
        code: 0,
        msg: '欢迎进入配种信息',
        data: {
            userInfo,  //用户信息
            countAll,  //总结果数
            length: result.length, //当页结果数
            result,    //用户羊只羊绒信息
        }
    })
})

//查询
router.use('/find',require('./controllers/find'))

//增加
router.use('/add',require('./controllers/add'))

//修改
router.use('/update',require('./controllers/update'))

module.exports = router
